package com.flyoil.petromp.ui.activity.activity_login;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;

import com.flyoil.petromp.Constant.ConstantNetworkCode;
import com.flyoil.petromp.R;
import com.flyoil.petromp.apiservice.HttpModel;
import com.flyoil.petromp.apiservice.OnObserverRetrofitResetCallBack;
import com.flyoil.petromp.base.BaseActivity;
import com.flyoil.petromp.base.BaseEntity;
import com.flyoil.petromp.entity.entity_me.UserMessageEntity;
import com.flyoil.petromp.ui.activity.activity_home.HomeActivity;
import com.flyoil.petromp.utils.Dialog.UpdataDialog;
import com.flyoil.petromp.utils.MoccaPreferences;

/**
 * 起始界面
 */
public class WelcomeActivity extends BaseActivity {

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initViews() {
        super.initViews();
        httpModel = new HttpModel();
    }

    @Override
    protected void initdatas() {
        super.initdatas();
        if (MoccaPreferences.user_login.get() == false) {
            startLoginActivity();
            return;
        }
        httpModel.getUserMessage(null, new OnObserverRetrofitResetCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity model) {
                super.onSuccess(model);
                UserMessageEntity entity = null;
                if (model instanceof UserMessageEntity)
                    entity = (UserMessageEntity) model;
                if (entity != null && entity.getCode() == ConstantNetworkCode.SuccedCode) {
                    MoccaPreferences.user_avatar.put(entity.getData().getAvatar());
                    MoccaPreferences.user_signature.put(entity.getData().getSignature());
                    if(entity.getData().getCompanyId()!=-1){
                        MoccaPreferences.companyId.put(entity.getData().getCompanyId());
                    }
                    MoccaPreferences.userId.put(entity.getData().getUserId());
                    startHomeActivity();
                } else {
                    startLoginActivity();
                }
            }

            @Override
            public void onFailure(String msg) {
                startLoginActivity();
            }
        });
    }

    private void startLoginActivity() {
        new Handler().postDelayed(new Runnable() {
            //            @Override
            public void run() {
                startActivity(new Intent(mContext, LoginActivity.class));
                finish();
            }
        }, 600);
    }
    private void startHomeActivity() {
        new Handler().postDelayed(new Runnable() {
            //            @Override
            public void run() {
                startActivity(new Intent(mContext, HomeActivity.class));
                finish();
            }
        }, 600);
    }

    /**
     * 显示APP版本升级更新的弹框
     */
    private void show_dialog() {
        UpdataDialog updataDialog = new UpdataDialog(this, R.style.progressHUD);
        updataDialog.setDialogContent("你好");
        updataDialog.setVersionCode("版本号：" + "1.0.3");
        updataDialog.setStorage("大小：" + "45M");
        updataDialog.setImageUrl("http://repair.flyoil.cn/apk/repairandroidbx.apk");
        updataDialog.show();
    }
}
